﻿using Autofac;
using Autofac.Integration.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Compilation;
using System.Web.Mvc;

namespace ZPJWeb.App_Start
{
    public class AutoFacConfig
    {
        #region 方法2
        public static void Register()
        {
            var builder = new ContainerBuilder();

            builder.RegisterControllers(Assembly.GetCallingAssembly())//注册mvc的Controller
                .PropertiesAutowired();//属性注入

            //1、无接口类注入
            //builder.RegisterType<BLL.newsClassBLL>().AsSelf().InstancePerRequest().PropertiesAutowired();

            //2、有接口类注入
            //注入BLL，UI中使用
            builder.RegisterAssemblyTypes(typeof(ZP.BLL.fz_sys_userService).Assembly)
                .AsImplementedInterfaces()  //是以接口方式进行注入
                .InstancePerRequest()       //每次http请求
                .PropertiesAutowired();     //属性注入

            //注入DAL，BLL层中使用
            builder.RegisterAssemblyTypes(typeof(ZP.DAL.fz_sys_userDAL).Assembly).AsImplementedInterfaces()
                .InstancePerRequest().PropertiesAutowired();     //属性注入

            //Cache的注入，使用单例模式
            //builder.RegisterType<RedisCacheManager>()
            //    .As<ICacheManager>()
            //    .SingleInstance()
            //    .PropertiesAutowired();

            //移除原本的mvc的容器，使用AutoFac的容器，将MVC的控制器对象实例交由autofac来创建
            var container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }
        #endregion

        #region 方法
        /*public static IContainer Instance;

        public static System.Web.Mvc.IDependencyResolver Init(Func<ContainerBuilder, ContainerBuilder> func = null)
        {
            var builder = new ContainerBuilder();//新建容器用于注册组件
            MyBuild(builder);//注册组件
            func?.Invoke(builder);
            Instance = builder.Build();//利用构建器创建容器
            return new AutofacDependencyResolver(Instance);//返回针对MVC的解析器
        }

        public static void MyBuild(ContainerBuilder builder)
        {
            //注册仓储层
            Assembly repositoryAssembly = Assembly.Load("ZP.DAL");
            builder.RegisterAssemblyTypes(repositoryAssembly)
                //.PublicOnly()   //只要public访问权限的
                .Where(cc => cc.IsClass)    //只要class类型的(排除值和interface类型)
                .AsImplementedInterfaces(); //自动以其实现的接口暴露(包括Dispose接口)
              

            //builder.RegisterAssemblyTypes(repositoryAssembly)//查找程序集中以Repository结尾的类型
            //.Where(t => t.Name.EndsWith("Service"))//查找所有程序集下面以Service结尾的类
            //.AsImplementedInterfaces(); //将找到的类和对应的接口放入IOC容器(放到IOC容器中有什么用处？：)


            //注册逻辑层
            Assembly serviceAssembly = Assembly.Load("ZP.BLL");
            builder.RegisterAssemblyTypes(serviceAssembly)
                //.PublicOnly()
                .Where(cc => cc.IsClass)
                .AsImplementedInterfaces();

            Assembly MvcAssembly = Assembly.Load("ZCSBZPJ");
            builder.RegisterControllers(MvcAssembly);//注册MVC项目中的Controller

        }*/
        #endregion
    }
}